//  Copyright (c) Whitemice Consulting, 2007, 2008, 2009
//  Adam Tauno Williams <awilliam@whitemice.org>
//  License: MIT/X11
// 
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
using System;
using System.Collections.Generic;
using Gtk;

namespace Whitemice.ZOGI.GtkWidgets
{
  public static class ListStoreExtensionMethods
  {
    public static bool Contains(this Gtk.ListStore list, object o)
    {
      TreeIter iter;
      if (list.GetIterFirst (out iter)) {
        do {
          object m = list.GetValue (iter, 1);
          if (o == m)
            return true;
        } while (list.IterNext (ref iter));
      }
      return false;
    }

    public static bool IterOf(this Gtk.ListStore list, object o, out TreeIter iter)
    {
      if (list.GetIterFirst (out iter)) {
        do {
          object m = list.GetValue (iter, 1);
          if (o == m)
            return true;
        } while (list.IterNext (ref iter));
      }
      return false;
    }

    public static bool AppendIfUnique(this Gtk.ListStore list, object o)
    {
      if (!(list.Contains(o)))
      {
        list.AppendValues(o);
        return true;
      } return false;
    }
    
    public static bool Remove(this Gtk.ListStore list, object o)
    {
      TreeIter iter;

      if (list.IterOf(o, out iter))
      {
        list.Remove(iter);
        return true;
      } return false;
    }

    public static bool GetValueAsType<T> (this Gtk.ListStore list, 
                                            TreeIter iter, 
                                            int column, 
                                            out T o) where T : class
    {
      o = null;
      var x = list.GetValue(iter, column);
      if ((x != null) && (x is T))
      {
        o = (x as T);
        return true;
      }
      return false;
    }

    public static bool GetValueAsType<T>(this Gtk.ListStore list,
                                            TreeIter iter,
                                            out T o) where T : class
    {
      return (list.GetValueAsType<T>(iter, 1, out o));
    }
  }
}
